import { login } from '@/api'
import { setToken, getToken, removeToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    userToken: getToken() || '',
    userName: ''
  },
  mutations: {
    SET_TOKEN(state, newToken) {
      state.userToken = newToken
      setToken(newToken)
    },
    SET_NAME(state, newName) {
      state.userName = newName
    },
    REMOVE_TOKEN(state) {
      state.userToken = ''
      state.userName = ''
      removeToken()
    }
  },
  actions: {
    async fetchLogin(store, loginForm) {
      const res = await login(loginForm)
      store.commit('SET_TOKEN', res.data.token)
      store.commit('SET_NAME', res.data.userName)
      // console.log(res.data)
      // console.log(store.state.userToken)
      // console.log(store.state.userName)
      return res
    }
  }
}
